import { isAuth } from "@/utils"
import { message } from "antd"
import { useEffect } from "react";
import { Redirect, Route, RouteProps } from "react-router-dom"

function PrivateRoute ({ children,component, ...rest }:RouteProps) {
  // 防止报错信息在useEffect中判断token是否存在
  useEffect(()=>{
    if(!isAuth()){
    message.error('请登陆')

    }    
  })
  
  return (
    <Route
      {...rest}
      render={({ location }) =>
       {
        if( isAuth()){  
          return(
           children || component
         ) }else{
           return (
             
           <Redirect
             to={{
               pathname: '/login',
               state: { from: location }
             }}
           />
         )
         } 
       }
      }
    />
  )
}
export default PrivateRoute
